package com.atwisdom.star.service.status;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.SpringContextUtil;
import com.atwisdom.star.core.dsl.modelClass.workCenter.Impl.WorkCenterInstanceDslServiceImpl;

/**
 * @ClassName: SparePartStatus
 * @ Description:工作中心状态管理
 * @author:王红艳
 * @date:2024/05/31 17:12
 */
public class WorkCenterStatus extends StatusMachine{
    private final WorkCenterInstanceDslServiceImpl workCenterInstanceDslServiceImpl  = SpringContextUtil.getBean(WorkCenterInstanceDslServiceImpl.class);

    public WorkCenterStatus(Long id){
        this.setId(id);
        this.initStatus();
        this.setStatus();
    }
    /**
     * 初始化状态
     */
    public void initStatus(){
        super.setStatusMap("创建","下派")
                .setStatusMap("下派","开工")
                .setStatusMap("开工","完工")
                .setStatusMap("开工","暂停")
                .setStatusMap("暂停","开工");

    }

    /**
     * 更新状态
     */
    @Override
    public void updateStatus() {
        JSONObject jsonObject = JsonUtil.buildJson(new Object[]{"id",this.getId(),"values",JsonUtil.buildJson(new Object[]{this.getUpdateKey(),this.getUpdateStatus()})});
        workCenterInstanceDslServiceImpl.updateById(jsonObject);
    }

    /**
     * 初始化状态
     */
    @Override
    public void setStatus(){
        JSONObject jsonObject = workCenterInstanceDslServiceImpl.queryJsonById(this.getId());
        this.setStatus(JsonUtil.get(jsonObject,"values.status"));
        this.setRunStatus(JsonUtil.get(jsonObject,"values.runStatus"));
    }

}
